import 'package:flutter/material.dart';

// import '../button/gkb_button.dart';
import 'gkb_dialog.dart' show GKBDialogArguments;

class GKBTipDialog {
  static void show(
    BuildContext context, {
    String? title,
    required String content,
    TextAlign? textAlign,
  }) {
    showDialog(
        context: context,
        barrierDismissible: false,
        routeSettings: RouteSettings(
            name: 'GKBTipDialog',
            arguments: GKBDialogArguments(title: title, content: content)),
        builder: (context) {
          return _TipWidget(
            title: title,
            content: content,
            textAlign: textAlign,
          );
        });
  }
}

class _TipWidget extends StatelessWidget {
  final String? title;
  final String content;
  final TextAlign? textAlign;

  const _TipWidget(
      {Key? key, this.title, required this.content, this.textAlign})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AnimatedPadding(
      padding: MediaQuery.of(context).viewInsets +
          const EdgeInsets.symmetric(horizontal: 40.0, vertical: 24.0),
      duration: const Duration(milliseconds: 20),
      curve: Curves.easeInCubic,
      child: MediaQuery.removeViewInsets(
        removeLeft: true,
        removeTop: true,
        removeRight: true,
        removeBottom: true,
        context: context,
        child: Center(
          child: SizedBox(
              width: 270.0,
              child: Stack(
                alignment: Alignment.topRight,
                clipBehavior: Clip.none,
                children: [
                  body(context),
                  Positioned(
                    right: -18,
                    top: -18,
                    child: closeBtn(context),
                  )
                ],
              )),
        ),
      ),
    );
  }

  Widget get titleWidget {
    return Offstage(
      offstage: title == null || title == '' ? true : false,
      child: Padding(
        padding: const EdgeInsets.only(left: 8, right: 8, top: 8),
        child: Text(
          title ?? '',
          style: const TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold),
        ),
      ),
    );
  }

  Widget body(BuildContext context) {
    return Material(
      borderRadius: BorderRadius.circular(8.0),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          // Row(
          //   mainAxisAlignment: MainAxisAlignment.end,
          //   crossAxisAlignment: CrossAxisAlignment.start,
          //   children: [
          //     Expanded(child: titleWidget),
          //   ],
          // ),
          titleWidget,
          Flexible(
              child: Padding(
            padding:
                const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
            child: Text(
              content,
              style: const TextStyle(fontSize: 14.0),
              textAlign: textAlign ?? TextAlign.center,
            ),
          )),
          const SizedBox(height: 8),
        ],
      ),
    );
  }

  Widget closeBtn(BuildContext context) {
    // return GKBCloseBtn(
    //   iconSize: 20,
    //   color: Colors.white,
    //   margin: const EdgeInsets.only(top: 10),
    //   border: Border.all(width: 1, color: Colors.white),
    // );
    return IconButton(
      constraints: const BoxConstraints(
        minHeight: 0,
      ),
      color: Colors.white,
      onPressed: () {
        Navigator.of(context).pop();
      },
      icon: Container(
        padding: const EdgeInsets.all(1),
        width: 20,
        height: 20,
        decoration: BoxDecoration(
          color: const Color(0xff666666),
          borderRadius: BorderRadius.circular(10),
        ),
        child: const Icon(
          Icons.close,
          size: 18,
        ),
      ),
    );
  }
}
